//
//  GiftPageView.m
//  MindBox
//
//  Created by MS on 16-1-8.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "GiftPageView.h"
#import "HomeGoodsCell.h"
#import "LGAdsView.h"

#import "Gift_SelView.h"
#import "Gift_GoodsView.h"
#import "Gift_LabelView.h"

#define giftSelHeiht 120

@interface GiftPageView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,LGAdsViewDelegate>

@property (nonatomic,weak) Gift_SelView * gift_SelView;
@property (nonatomic,weak) Gift_GoodsView * gift_goodsView;
@property (nonatomic,weak) Gift_LabelView * gift_labelView;
@property (nonatomic,weak) LGAdsView * gift_adsView;

@end

@implementation GiftPageView

-(void)setGiftPageData:(GiftPageData *)giftPageData
{
    _giftPageData = giftPageData;
    self.gift_adsView.giftpageData = giftPageData;
    self.gift_labelView.giftPageData = giftPageData;
    self.gift_goodsView.giftPageData = giftPageData;
    self.gift_SelView.giftData = giftPageData;
    self.bottomView.title.text = giftPageData.search_title;
    self.gift_selectView.itemsArray = giftPageData.gift_cat_id;
    [self layoutSubviews];
    [self.collectionView reloadData];
}

-(void)setGift_SelData:(NSArray *)gift_SelData
{
    _gift_SelData = gift_SelData;

    self.gift_SelView.gift_SelData = gift_SelData;
    
    [self.collectionView reloadData];
    [self layoutSubviews];
    [self.collectionView reloadData];

}


-(void)setGift_ItemGoods:(NSArray *)gift_ItemGoods
{
    _gift_ItemGoods = gift_ItemGoods;
    
    [self.collectionView reloadData];
}


-(LGAdsView *)gift_adsView
{
    if (_gift_adsView==nil) {
        LGAdsView * view = [[LGAdsView alloc] init];
        _gift_adsView = view;
        [self.collectionView addSubview:view];
        _gift_adsView.delegate = self;
    }
    return _gift_adsView;
}

-(Gift_LabelView *)gift_labelView
{
    if (_gift_labelView==nil) {
        Gift_LabelView * view = [[Gift_LabelView alloc] init];
        _gift_labelView =  view;
        [self.collectionView addSubview:view];
        
        __weak typeof(self) weakSelf = self;
        
        [_gift_labelView setLabelViewIndexBlock:^(NSInteger index) {
            
            if ([weakSelf.delegate respondsToSelector:@selector(giftLabelViewDidClick:label:index:)]) {
                 [weakSelf.delegate giftLabelViewDidClick:weakSelf label:weakSelf.giftPageData.label[index] index:index];
            }
           
        }];
        
    }
    return _gift_labelView;
}

-(void)dealloc
{
//    NSLog(@"GiftPageView销毁");
}

-(Gift_GoodsView *)gift_goodsView
{
    if (_gift_goodsView==nil) {
        Gift_GoodsView * view = [Gift_GoodsView gift_GoodsViewWithXib];
        _gift_goodsView = view;
        [self.collectionView addSubview:view];
        _gift_goodsView.frame = CGRectMake(0, 0, self.width, self.width);
        
        __weak typeof(self) weakSelf = self;
        [_gift_goodsView setGoodsViewIndexBlock:^(NSInteger index) {
            
            if ([weakSelf.delegate respondsToSelector:@selector(giftReguessGoodsViewDidClick:goods:index:)]) {
                
                [weakSelf.delegate giftReguessGoodsViewDidClick:weakSelf goods:weakSelf.giftPageData.goods[index] index:index];
            }
            
        }];
   
    }
    return _gift_goodsView;
}

-(Gift_SelView *)gift_SelView
{
    if (_gift_SelView==nil) {
        Gift_SelView * view = [Gift_SelView gift_SelViewWithXib];
        view.frame =  CGRectMake(0, CGRectGetMaxY(_gift_goodsView.frame), self.width, self.gift_SelData.count * (giftSelHeiht+10)+30);
        
        _gift_SelView = view;
        [self.collectionView addSubview:view];
        
        __weak typeof(self) weakSelf = self;
        
        [_gift_SelView setSelViewIndexBlock:^(NSInteger index) {
            if ([weakSelf.delegate respondsToSelector:@selector(giftSelViewDidClick:selTopic:index:)]) {
                
                [weakSelf.delegate giftSelViewDidClick:weakSelf selTopic:weakSelf.gift_SelData[index] index:index];
            }
            
        }];

        
    }
    return _gift_SelView;
}

-(BottomView *)bottomView
{
    if (_bottomView==nil) {
        BottomView * view = [BottomView bottomViewWithXib];
        _bottomView = view;
        _bottomView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
       
        [self.collectionView addSubview:view];
    }
    return _bottomView;
}

-(SelectView *)gift_selectView
{
    if (_gift_selectView==nil) {
        SelectView * view =[[SelectView alloc] init];
        _gift_selectView = view;
        [self.collectionView addSubview:view];
        _gift_selectView.delegate = self;
    }
    return _gift_selectView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UICollectionViewFlowLayout * viewLayout = [[UICollectionViewFlowLayout alloc] init];
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        viewLayout.minimumLineSpacing = 10;
        viewLayout.minimumInteritemSpacing = 0;
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:viewLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.alwaysBounceVertical = YES;
        self.collectionView = collectionView;
        
        collectionView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [self addSubview:collectionView];
        
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.gift_ItemGoods.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGoodsCell * cell = [HomeGoodsCell cellWithcollectionView:collectionView cellForItemAtIndexPath:indexPath];
    if (self.gift_ItemGoods.count) {
           cell.giftGoods = self.gift_ItemGoods[indexPath.item];
    }
 
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake((self.width - 30)/2, 220);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
  
    return UIEdgeInsetsMake(CGRectGetMaxY(_bottomView.frame)+_gift_selectView.height+10, 10, 0, 10);
    
    /**
     * _gift_selectView.frame设置动态变化，不可用以下设置
     */
//    return UIEdgeInsetsMake(CGRectGetMaxY(_gift_selectView.frame)+10, 10, 0, 10);
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /**
     *  设置选择条悬停
     */
    if (scrollView.contentOffset.y>= CGRectGetMaxY(_bottomView.frame)) {
     
        _gift_selectView.y =scrollView.contentOffset.y;
    }
    
    else{
            _gift_selectView.y = CGRectGetMaxY(_bottomView.frame);
        }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _gift_adsView.frame = CGRectMake(0, 0, self.width, 190);
    
    _gift_labelView.frame = CGRectMake(0, CGRectGetMaxY(_gift_adsView.frame)+10, self.width, 120);
    
    _gift_goodsView.frame = CGRectMake(0, CGRectGetMaxY(_gift_labelView.frame)+10, self.width, self.width);
    _gift_SelView.frame =  CGRectMake(0, CGRectGetMaxY(_gift_goodsView.frame)+10, self.width, self.gift_SelData.count * (giftSelHeiht+10)+30);
    _bottomView.frame = CGRectMake(0, CGRectGetMaxY(_gift_SelView.frame), self.width, 30);
    
    _gift_selectView.frame =CGRectMake(0, CGRectGetMaxY(_bottomView.frame), self.width, 30);
}

#pragma 代理方法传参
-(void)adsViewDidClick:(LGAdsView *)adsView index:(NSInteger)index
{
   Gift_Banner * banner = self.giftPageData.banner[index];
    if ([self.delegate respondsToSelector:@selector(giftAdsViewDidClick:banner:index:)]) {
        [self.delegate giftAdsViewDidClick:self banner:banner index:index];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.delegate respondsToSelector:@selector(giftCollectionViewDidClick:itemGoods:index:)]) {
        [self.delegate giftCollectionViewDidClick:self itemGoods:self.gift_ItemGoods[indexPath.item] index:indexPath.item];
    }
    
}

-(void)selectViewDidClick:(SelectView *)selectView index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(giftSelectedViewDidClick:selectItem:index:)]) {
        [_delegate giftSelectedViewDidClick:self selectItem:self.giftPageData.gift_cat_id[index] index:index];
    }
    
    /**
     *  选择条悬停时点击回到头部位置
     */
    if (selectView.y>CGRectGetMaxY(_bottomView.frame)) {
        [self.collectionView setContentOffset:CGPointMake(0, CGRectGetMaxY(_bottomView.frame)) animated:YES];
    }
}

@end
